package com.sczmgk.hotline.db.impl;

import android.content.Context;
import android.database.Cursor;

import com.sczmgk.hotline.db.ChatMsgDao;
import com.sczmgk.hotline.db.base.DAOSupport;
import com.sczmgk.hotline.db.base.DBHelper;
import com.sczmgk.hotline.entity.ChatMsgBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenb on 2016/12/6.
 */

public class ChatMsgDaoImpl extends DAOSupport<ChatMsgBean> implements ChatMsgDao {

    private ChatMsgDaoImpl(Context context) {
        super(context);
    }
    private static ChatMsgDaoImpl intance;
    public static ChatMsgDaoImpl getIntance(Context context){
        if(intance == null){
            intance = new ChatMsgDaoImpl(context);
        }
        return intance;
    }

    /**
     * 分页加载数据
     * @param value  用户id
     * @param startIndex  开始的索引
     * @param maxNumber 加载的条数
     * @return
     */

    public List<ChatMsgBean> findByLimit(Serializable value, Serializable startIndex, Serializable maxNumber,Serializable isOnline){
        //TODO  asc 升序 desc 降序
//        String sql = "select * from " + getTableName() + " where "+ DBHelper.USERID+"=?"+" order by "+DBHelper.MSGTIME+" desc" +" limit ? "+" offset ?";
        String sql = "select * from " + getTableName() + " where "+ DBHelper.USERID+"=?"+" and "+DBHelper.ISONLINE+"=?"+" order by "+DBHelper.MSGTIME+" desc" +" limit ? "+" offset ?";
        Cursor cursor = db.rawQuery(sql, new String[] {value+"",isOnline+"",maxNumber+"",startIndex+""});
        if (cursor != null) {
            List<ChatMsgBean> result = new ArrayList<>();
            while (cursor.moveToNext()) {
                ChatMsgBean instance = fillInstance(cursor);
                result.add(instance);
            }
            cursor.close();
            return result;
        }
        return null;
    }


    /**
     * 分页加载数据
     * @param value  imei
     * @param startIndex  开始的索引
     * @param maxNumber 加载的条数
     * @return
     */

    public List<ChatMsgBean> findByLimitForImei(Serializable value, Serializable startIndex, Serializable maxNumber,Serializable isOnline){
        //TODO  asc 升序 desc 降序
//        String sql = "select * from " + getTableName() + " where "+ DBHelper.USERID+"=?"+" order by "+DBHelper.MSGTIME+" desc" +" limit ? "+" offset ?";
        String sql = "select * from " + getTableName() + " where "+ DBHelper.TYPENAME+"=?"+" and "+DBHelper.ISONLINE+"=?"+" order by "+DBHelper.MSGTIME+" desc" +" limit ? "+" offset ?";
        Cursor cursor = db.rawQuery(sql, new String[] {value+"",isOnline+"",maxNumber+"",startIndex+""});
        if (cursor != null) {
            List<ChatMsgBean> result = new ArrayList<>();
            while (cursor.moveToNext()) {
                ChatMsgBean instance = fillInstance(cursor);
                result.add(instance);
            }
            cursor.close();
            return result;
        }
        return null;
    }

    /**
     * 分页加载数据
     * @param value  用户id
     * @return
     */
    public List<ChatMsgBean> getWaitUploadList(Serializable value){
        //TODO  asc 升序 desc 降序
//        String sql = "select * from " + getTableName() + " where "+ DBHelper.USERID+"=?"+" order by "+DBHelper.MSGTIME+" desc" +" limit ? "+" offset ?";
        String sql = "select * from " + getTableName() + " where "+ DBHelper.USERID+"=?"+" and "+DBHelper.NEED_UPLOAD+"=1";
        Cursor cursor = db.rawQuery(sql, new String[] {value+""});
        if (cursor != null) {
            List<ChatMsgBean> result = new ArrayList<>();
            while (cursor.moveToNext()) {
                ChatMsgBean instance = fillInstance(cursor);
                result.add(instance);
            }
            cursor.close();
            return result;
        }
        return null;
    }

    public int deleteMsgByChatid(Serializable chatid){

       return db.delete(getTableName(), DBHelper.CHATID + "=?", new String[]{chatid + ""});
    }
}
